package com.bittech.httpd.core;

import com.bittech.httpd.common.HttpMethod;
import com.bittech.httpd.common.HttpRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: secondriver
 * Created: 2018/9/5
 */
public class DefaultHttpRequest implements HttpRequest {
    
    private final Map<String, String> line;
    
    private final Map<String, String> header;
    
    private final Map<String, List<String>> params;
    
    private Map<String, Object> context = new HashMap<>();
    
    public DefaultHttpRequest(Map<String, String> line, Map<String, String> header, Map<String, List<String>> params) {
        this.line = line;
        this.header = header;
        this.params = params;
    }
    
    public void setContext(Map<String, Object> context) {
        this.context = context;
    }
    
    @Override
    public Object getContextValue(String key) {
        return context.get(key);
    }
    
    @Override
    public HttpMethod method() {
        return HttpMethod.lookup(line.get("method"));
    }
    
    @Override
    public String uri() {
        return line.get("url");
    }
    
    @Override
    public String version() {
        String version = line.get("version");
        return version == null ? "HTTP/1.1" : version;
    }
    
    @Override
    public Map<String, String> header() {
        return header;
    }
    
    @Override
    public Map<String, List<String>> params() {
        return params;
    }
}
